package com.ledi.guns.modular.ledipay.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.plugins.Page;
import com.ledi.guns.common.exception.AppCheckException;
import com.ledi.guns.common.persistence.dao.DeptMapper;
import com.ledi.guns.common.persistence.dao.FeedbackImgMapper;
import com.ledi.guns.common.persistence.dao.FeedbackMapper;
import com.ledi.guns.common.persistence.dao.RoleMapper;
import com.ledi.guns.common.persistence.dao.UserMapper;
import com.ledi.guns.common.persistence.model.Dept;
import com.ledi.guns.common.persistence.model.Feedback;
import com.ledi.guns.common.persistence.model.FeedbackImg;
import com.ledi.guns.common.persistence.model.Role;
import com.ledi.guns.common.persistence.model.User;
import com.ledi.guns.common.persistence.model.vo.FeedBackVo;
import com.ledi.guns.common.persistence.model.vo.UserVo;
import com.ledi.guns.core.shiro.ShiroKit;
import com.ledi.guns.core.shiro.ShiroUser;
import com.ledi.guns.core.util.DateUtil;
import com.ledi.guns.core.util.SystemConstans;
import com.ledi.guns.modular.ledipay.controller.req.feedback.FeedBackAdd;
import com.ledi.guns.modular.ledipay.controller.req.feedback.FeedBackForm;
import com.ledi.guns.modular.ledipay.service.FeedbackService;

/**
 * 文件名称： com.ledi.guns.modular.ledipay.service.impl.FeedbackServiceImpl.java<br/>
 * 初始作者： 钟小平<br/>
 * 创建日期： 2018年7月4日<br/>
 * 功能说明： 这里用一句话描述这个类的作用--此句话需删除 <br/>
 * =================================================<br/>
 * 修改记录：<br/>
 * 修改作者 日期 修改内容<br/>
 * ================================================<br/>
 * Copyright (乐迪教育科技有限公司-幼儿事业部) 2017-2018.All rights reserved.<br/>
 */
@Service
public class FeedbackServiceImpl implements FeedbackService {

	@Autowired
	private FeedbackMapper feedbackMapper;

	@Autowired
	private FeedbackImgMapper feedbackImgMapper;
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private DeptMapper deptMapper;

	@Autowired
	private RoleMapper roleMapper;

	/**
	 * 方法描述: [反馈列表.]<br/>
	 * 初始作者: 钟小平<br/>
	 * 创建日期: 2018年7月4日-下午3:08:31<br/>
	 * 开始版本: 2.0.0<br/>
	 * =================================================<br/>
	 * 修改记录：<br/>
	 * 修改作者 日期 修改内容<br/>
	 * ================================================<br/>
	 */
	@Override
	public Page<FeedBackVo> pageList(Page<FeedBackVo> page, FeedBackForm query) {

		extracted(query);
		List<FeedBackVo> list = feedbackMapper.pageList(page, query.getSearch());
		list.forEach(x -> {
			Map<String, Object> columnMap = new HashMap<String, Object>();
			columnMap.put("feedback_id", x.getId());
			List<FeedbackImg> selectByMap = feedbackImgMapper.selectByMap(columnMap);
			List<String> imageUrl = selectByMap.stream().map(FeedbackImg::getImageUrl).collect(Collectors.toList());
			x.setImageUrl(imageUrl);
			UserVo schoolUser = userMapper.getSchoolUser(x.getCreatorId());
			Role role = roleMapper.selectById(x.getRoleId());
			if (role != null) {
				Dept selectById = deptMapper.selectById(role.getDeptid());
				if (selectById != null) {
					String fullname = selectById.getFullname();
					switch (fullname) {
						case SystemConstans.DEPT_SCHOOL:
							x.setCreatorName(schoolUser.getSchoolName());
							// x.setContact(schoolUser.getPhone());
							x.setAddMan(null);
							break;
						case SystemConstans.DEPT_CUSTOMER:
							x.setCreatorName(schoolUser.getName());
							// x.setContact(schoolUser.getContactTel());
							x.setAddMan(null);
							break;
						default:
							break;
					}
				}
			}
			switch (x.getStatus()) {
				case 0:
					x.setStatusZh(SystemConstans.STATUS_0);
					break;
				case 1:

					User selectById = userMapper.selectById(x.getUpdaterId());
					if (selectById != null) {
						x.setUpdaterName(selectById.getName());
					}
					x.setStatusZh(SystemConstans.STATUS_1);
					break;

				default:
					break;
			}
			switch (x.getType()) {
				case 0:
					x.setTypeZh(SystemConstans.FEED_TYPE_0);
					break;
				case 1:
					x.setTypeZh(SystemConstans.FEED_TYPE_1);
					break;
				case 2:
					x.setTypeZh(SystemConstans.FEED_TYPE_2);
					break;

				default:
					break;
			}

		});

		page.setRecords(list);
		return page;
	}

	private void extracted(FeedBackForm query) {

		if (query.getStartTime() != null) {
			query.setStartTime(DateUtil.formate(query.getStartTime(), true));
		}
		if (query.getEndTime() != null) {
			query.setEndTime(DateUtil.formate(query.getEndTime(), false));
		}
	}

	/**
	 * 方法描述: [处理反馈意见.]<br/>
	 * 初始作者: 钟小平<br/>
	 * 创建日期: 2018年7月5日-下午1:31:29<br/>
	 * 开始版本: 2.0.0<br/>
	 * =================================================<br/>
	 * 修改记录：<br/>
	 * 修改作者 日期 修改内容<br/>
	 * ================================================<br/>
	 */
	@Override
	@Transactional
	public void addorUpdateFeedback(FeedBackAdd feedBackAdd) {

		ShiroUser currentUser = ShiroKit.getUser();

		User oneUsers = userMapper.selectById(currentUser.getId());
		if (1 != oneUsers.getStatus()) {
			throw new AppCheckException("school.user.notActive.code", "school.user.notActive.message");
		}

		if (feedBackAdd.getId() == null) {
			// 获取菜单列表
			List<Integer> roleList = ShiroKit.getUser().getRoleList();
			if (roleList == null || roleList.size() == 0) {
				ShiroKit.getSubject().logout();
				throw new AppCheckException("system.user.role.code", "system.user.role.message");
			}
			Integer roleId = feedBackAdd.getRoleId();
			if (roleList.indexOf(roleId) < 0) {
				throw new AppCheckException("system.user.role.notFind.code", "system.user.role.notFind.message");
			}
			Feedback entity = new Feedback();
			entity.setCreateTime(new Date());
			entity.setCreatorId(currentUser.getId());
			BeanUtils.copyProperties(feedBackAdd, entity);
			entity.setAddMan(currentUser.getName());
			if (feedBackAdd.getStatus().equals(SystemConstans.YES)) {
				if (feedBackAdd.getIdea().length() > 500) {
					throw new AppCheckException("feedBack.idea.overflow.code", "feedBack.idea.overflow.message");
				}
				entity.setUpdaterId(currentUser.getId());
				entity.setUpdateTime(new Date());
			}
			feedbackMapper.insert(entity);
			List<String> imageUrl = feedBackAdd.getImageUrl();
			if (CollectionUtils.isNotEmpty(imageUrl)) {
				List<FeedbackImg> imageList = new ArrayList<FeedbackImg>();
				for (String image : imageUrl) {
					FeedbackImg feedbackImg = new FeedbackImg();
					feedbackImg.setFeedbackId(entity.getId());
					feedbackImg.setImageUrl(image);
					imageList.add(feedbackImg);
				}
				feedbackImgMapper.insertBatch(imageList);
			}

		} else {
			Feedback selectById = feedbackMapper.selectById(feedBackAdd.getId());
			selectById.setUpdaterId(currentUser.getId());
			selectById.setUpdateTime(new Date());
			selectById.setStatus(SystemConstans.YES);
			selectById.setVersionNo(feedBackAdd.getVersionNo());
			selectById.setIdea(feedBackAdd.getIdea());
			selectById.setUpdateName(currentUser.getName());
			feedbackMapper.updateById(selectById);

		}

	}

	/**
	 * 方法描述: [获取反馈详情.]<br/>
	 * 初始作者: 钟小平<br/>
	 * 创建日期: 2018年7月5日-下午3:30:40<br/>
	 * 开始版本: 2.0.0<br/>
	 * =================================================<br/>
	 * 修改记录：<br/>
	 * 修改作者 日期 修改内容<br/>
	 * ================================================<br/>
	 */
	@Override
	public FeedBackVo findFeedBack(Integer id) {

		FeedBackVo vo = new FeedBackVo();
		Feedback selectById = feedbackMapper.selectById(id);
		BeanUtils.copyProperties(selectById, vo);
		Map<String, Object> columnMap = new HashMap<String, Object>();
		columnMap.put("feedback_id", id);
		List<FeedbackImg> selectByMap = feedbackImgMapper.selectByMap(columnMap);
		if (selectByMap != null) {
			List<String> imageUrl = selectByMap.stream().map(FeedbackImg::getImageUrl).collect(Collectors.toList());
			vo.setImageUrl(imageUrl);
		}
		switch (vo.getStatus()) {
			case 0:
				vo.setStatusZh(SystemConstans.STATUS_0);
				break;
			case 1:
				User u1 = userMapper.selectById(vo.getUpdaterId());
				if (u1 != null) {
					vo.setUpdaterName(u1.getName());
				}
				vo.setStatusZh(SystemConstans.STATUS_1);
				break;

			default:
				break;
		}
		switch (vo.getType()) {
			case 0:
				vo.setTypeZh(SystemConstans.FEED_TYPE_0);
				break;
			case 1:
				vo.setTypeZh(SystemConstans.FEED_TYPE_1);
				break;
			case 2:
				vo.setTypeZh(SystemConstans.FEED_TYPE_2);
				break;

			default:
				break;
		}
		UserVo schoolUser = userMapper.getSchoolUser(vo.getCreatorId());
		Role role = roleMapper.selectById(vo.getRoleId());
		if (role != null) {
			Dept dept = deptMapper.selectById(role.getDeptid());
			if (dept != null) {
				String fullname = dept.getFullname();
				switch (fullname) {
					case SystemConstans.DEPT_SCHOOL:
						vo.setCreatorName(schoolUser.getSchoolName());
						vo.setAddMan(null);
						break;
					case SystemConstans.DEPT_CUSTOMER:
						vo.setCreatorName(schoolUser.getName());
						vo.setAddMan(null);
						break;
					default:
						break;
				}
			}
		}
		return vo;
	}

}
